package io.mft.commons.util;

import java.io.File;
import java.net.InetAddress;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.mft.commons.bean.MachineHostAddress;
import io.mft.commons.util.xml.XmlLoader;
import io.mft.commons.util.xml.XmlLoader.NodeWrapper;

public class MachineAddressUtil {
	static Logger logger=LoggerFactory.getLogger(MachineAddressUtil.class);
	public static MachineHostAddress getMachineHostAddress() {
		try {
			MachineHostAddress machineHostAddress=new MachineHostAddress();
			 InetAddress addr = InetAddress.getLocalHost();  
	         String ip=addr.getHostAddress().toString(); //获取本机ip  
	         String hostName=addr.getHostName().toString(); //获取本机计算机名称  
			 machineHostAddress.setIp(ip);
			 machineHostAddress.setHostName(hostName);
			URL url=XmlLoader.class.getClassLoader().getResource(".");
			String path=null;
			if (url==null) {
				path=System.getProperty("user.dir");
			}else {
				File file=new File(url.getFile());
				path=file.getParent();
			}
			path=path+File.separator+"TBAServer.xml";
			XmlLoader.instance.loadSystemDirXmlNodes(path);
			NodeWrapper nodeWrapper=XmlLoader.instance.getNodeWrapper("TBAServer.xml.Server.ListenPort");
			machineHostAddress.setPort(nodeWrapper.getText());
			return machineHostAddress;
		} catch (Exception e) {
			logger.error("", e);
			return null;
		}

	}
}
